<?php
class Soul_Event_Manager{
	
	private static $_eventListeners = array();
	
	public static function registerListener($dispatcher, $receiver){
		if(!isset(self::$_eventListeners[$dispatcher])){
			self::$_eventListeners[$dispatcher] = array();
		}
		self::$_eventListeners[$dispatcher][] = $receiver;
	}
	
	public static function removeListener($dispatcher, $receiver){
		$receivers = self::getReceivers($dispatcher);
		foreach($receivers as $key=>$values){
			unset($receivers[$key]);
			return;
		}
	}
	
	public static function callReceiver($dispatcher, $argument = NULL){
		$receivers = self::getReceivers($dispatcher);
		if(!is_null($receivers)){
			foreach($receivers as $receiver){
				if(is_array($receiver)){
					$className = $receiver[0];
					$methodName = $receiver[1];
					Soul_Loader::loadSingleton($className)->$methodName($argument);						
				}else{
					$receiver($argument);				
				}
			}
		}
	}
	
	public static function getReceivers($dispatcher){
		if(isset(self::$_eventListeners[$dispatcher])){
			return self::$_eventListeners[$dispatcher];
		}		
		return NULL;
	}
}